﻿Imports CupsApp.Data
Imports CupsApp.Service

Partial Public Class manage_landlords
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        CType(Master.FindControl("lnkNewLandlords"), LinkButton).Enabled = False
        CType(Master.FindControl("lnkNewLandlords"), LinkButton).Text = "> Landlords <"
        lvlMsg.Text = ""
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click

        lvlMsg.Text = ""
        If btnSave.Text = "Save" Then
            SaveLandlord()
        Else
            UpdateLandlord()
        End If

        ClearField()

    End Sub

    Public Sub UpdateLandlord()

        Dim cds As New CupsDomainService()
        Dim objLandlord As New cups_Landlord
        Dim Name As String = ""
        Dim PhoneNo As String = ""
        Dim CompanyName As String = ""
        Dim Notes As String = ""

        Try

            Name = txtName.Text.Trim
            PhoneNo = txtPhone.Text.Trim
            CompanyName = txtCompanyName.Text.Trim
            Notes = txtNotes.Text.Trim

            With objLandlord
                .Name = Name
                .Phone = PhoneNo
                .UpdatedBy = cds.GetCurrentUser
                .UpdatedOn = Date.Now()
                .CompanyName = CompanyName           
                .Notes = Notes
            End With

            cds.UpdateLandlord(objLandlord, ViewState("LandlordID"))

            gvLandlords.DataBind()

            btnClear.Text = "Clear Fields"
            btnSave.Text = "Save"
            gvLandlords.Enabled = True

            lvlMsg.Text = "* Landlord information was updated successfully!"

        Catch ex As Exception
            lvlMsg.ForeColor = Drawing.Color.Red
            lvlMsg.Text = "* Error: Landlord information can not be updated!"
        End Try

    End Sub


    Public Sub SaveLandlord()

        Dim cds As New CupsDomainService()
        Dim objLandlord As New cups_Landlord
        Dim Name As String = ""
        Dim PhoneNo As String = ""
        Dim CompanyName As String = ""
        Dim Notes As String = ""

        Try

            Name = txtName.Text.Trim
            PhoneNo = txtPhone.Text.Trim
            CompanyName = txtCompanyName.Text.Trim
            Notes = txtNotes.Text.Trim

            With objLandlord
                .Name = Name
                .Phone = PhoneNo
                .UpdatedBy = cds.GetCurrentUser
                .UpdatedOn = Date.Now()
                .CompanyName = CompanyName
                .Notes = Notes
            End With

            cds.SaveLandlord(objLandlord)

            gvLandlords.DataBind()
            lvlMsg.Text = "* Landlord information was save successfully!"
        Catch ex As Exception
            lvlMsg.ForeColor = Drawing.Color.Red
            lvlMsg.Text = "* Error: Landlord information can not be saved!"
        End Try

    End Sub

    Public Sub ClearField()

        txtName.Text = ""
        txtPhone.Text = ""
        txtCompanyName.Text = ""
        txtNotes.Text = ""

    End Sub

    Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClear.Click

        If btnClear.Text = "Cancel" Then
            btnClear.Text = "Clear Fields"
            btnSave.Text = "Save"
            gvLandlords.Enabled = True
        End If

        lvlMsg.Text = ""
        ClearField()

    End Sub

    Protected Sub gvLandlords_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles gvLandlords.RowCommand
        If e.CommandName = "Modify" Then
            Dim rowIndex As Integer = Integer.Parse(e.CommandArgument.ToString())
            Dim LandlordID As Integer = Convert.ToInt64(gvLandlords.Rows(rowIndex).Cells(1).Text)
            btnSave.Text = "Update"
            btnClear.Text = "Cancel"
            ViewState("LandlordID") = LandlordID
            FillValue(LandlordID)

            gvLandlords.Enabled = False

        End If
    End Sub

    Public Sub FillValue(ByVal LandlordID As Integer)

        Dim cds As New CupsDomainService()
        Dim objLandlord As New cups_Landlord

        objLandlord = cds.GetLandlordForEdit(LandlordID)

        With objLandlord
            txtName.Text = .Name
            txtPhone.Text = .Phone
            txtCompanyName.Text = .CompanyName
            txtNotes.Text = .Notes
        End With

    End Sub

End Class